package com.weiiew.backend.operation;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.io.Serializable;
import java.util.HashMap;

/**
 * @Description 用于描述电脑操作的基类
 * @Author 魏锴
 * @Date 2025/4/26
 */
@Getter
@Setter
public abstract class ComputerOperation implements Serializable {
    private static final HashMap<Class<?>, String> classMap = new HashMap<>();

    static {
        classMap.put(ComputerOperation.class, "bs");
        classMap.put(CombinedKeyClick.class, "ck");
        classMap.put(DelayOperation.class, "do");
        classMap.put(KeyPress.class, "kp");
        classMap.put(KeyRelease.class, "kr");
        classMap.put(MouseMove.class, "mm");
        classMap.put(MousePress.class, "mp");
        classMap.put(MouseRelease.class, "mr");
        classMap.put(MouseWheelScroll.class, "wc");
    }// 定义反序列化逻辑映射

    protected long timestamp; // 操作发生时间（从录制开始计算的偏移量）

    public void execute(Robot robot) {

    }

    // 新增方法：返回当前类的简单名称（如 "KeyPress"）
    @JsonProperty("type") // 确保该字段被序列化为 className
    public String getClassName() {
        return classMap.get(this.getClass());
    }

    public static ComputerOperation deserialize(String description) {
        String[] strings = description.split("@");
        switch (strings[0]) {
            case "kp":
                return new KeyPress(Long.parseLong(strings[1]), strings[2]);
            case "kr":
                return new KeyRelease(Long.parseLong(strings[1]), strings[2]);
            case "mm":
                return new MouseMove(Long.parseLong(strings[1]), strings[2]);
            case "mp":
                return new MousePress(Long.parseLong(strings[1]), strings[2]);
            case "mr":
                return new MouseRelease(Long.parseLong(strings[1]), strings[2]);
            case "wc":
                return new MouseWheelScroll(Long.parseLong(strings[1]), strings[2]);
            default:
                return new UnknownOperation();
        }
    }

    public static String serialize(ComputerOperation operation) {
        if (operation instanceof UnknownOperation) {
            return "unknown";
        }
        return operation.toDescriptionString();
    }

    public String toDescriptionString() {
        return "unknown";
    }

}